//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.planw.beetl.utils;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.lang3.StringUtils;

public class StrUtil {
  private static String[] validStr = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "_", "."};
  private static char[][] validChars;

  public StrUtil() {
  }

  public static boolean searchCharSequence(String str, String search) {
    if (StringUtils.equals(search, "*")) {
      return true;
    } else {
      AtomicInteger postion = new AtomicInteger(0);
      search = StringUtils.lowerCase(search);
      str = StringUtils.lowerCase(str);
      for (int i = 0; i < search.length(); ++i) {
        if (postion.intValue() != -1) {
          postion.set(str.indexOf(search.charAt(i), postion.get()));
        }
      }
      return postion.intValue() != -1;
    }
  }

  public static boolean containAny(String str, String... searchStr) {
    for(String search : searchStr) {
      if (search != null && !search.trim().isEmpty() && str.contains(search)) {
        return true;
      }
    }

    return false;
  }

  public static String findBeetlSqlVarSubStr(String text, int column, BeetlSqlConst beetlSqlConst) {
    if (text != null && !text.trim().isEmpty()) {
      boolean isBeetlSqlContent = containAny(text, beetlSqlConst.DELIMITER_STATEMENT_START, beetlSqlConst.DELIMITER_PLACEHOLDER_START, beetlSqlConst.DELIMITER_PLACEHOLDER_START2);
      if (!isBeetlSqlContent) {
        return "";
      } else {
        char[] chars = text.toCharArray();
        int start = -1;

        for(int i = column; i >= 0; --i) {
          boolean flag = false;

          for(char[] validChar : validChars) {
            int validCharLength = validChar.length;
            int from = Math.max(i - validCharLength, 0);
            char[] copyOfRange = Arrays.copyOfRange(chars, from, i);
            if (charsEqual(validChar, copyOfRange)) {
              start = from;
              flag = true;
              break;
            }
          }

          if (!flag) {
            break;
          }
        }

        return start == -1 ? "" : new String(Arrays.copyOfRange(chars, start, column));
      }
    } else {
      return "";
    }
  }

  private static boolean charsEqual(char[] chars1, char[] chars2) {
    if (chars1.length != chars2.length) {
      return false;
    } else {
      int min = chars1.length;

      for(int i = 0; i < min; ++i) {
        if (chars1[i] != chars2[i]) {
          return false;
        }
      }

      return true;
    }
  }

  static {
    validChars = new char[validStr.length][];

    for(int i = 0; i < validStr.length; ++i) {
      validChars[i] = validStr[i].toCharArray();
    }

  }
}
